<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="考试名称" prop="examId">
              <el-input
                v-model="dataForm.examId"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="课程名称" prop="courseId">
              <el-input
                v-model="dataForm.courseId"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="试卷名称" prop="paperId">
              <el-input
                v-model="dataForm.paperId"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                v-model="dataForm.startTime"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="提交时间" prop="submitTime">
              <el-date-picker
                v-model="dataForm.submitTime"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型" prop="submitType">
              <el-select
                v-model="dataForm.submitType"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in submitTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="批阅状态" prop="questionReviewStatus">
              <el-switch
                v-model="dataForm.questionReviewStatus"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="批阅人" prop="reviewerUserId">
              <el-input
                v-model="dataForm.reviewerUserId"
                placeholder="系统自动生成"
                readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="批阅时间" prop="reviewerTime">
              <el-input
                v-model="dataForm.reviewerTime"
                placeholder="系统自动生成"
                readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="创建用户" prop="creatorUserId">
              <el-input
                v-model="dataForm.creatorUserId"
                placeholder="系统自动生成"
                readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="创建时间" prop="creatorTime">
              <el-input
                v-model="dataForm.creatorTime"
                placeholder="系统自动生成"
                readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="修改用户" prop="lastModifyUserId">
              <el-input
                v-model="dataForm.lastModifyUserId"
                placeholder="系统自动生成"
                readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="修改时间" prop="lastModifyTime">
              <el-input
                v-model="dataForm.lastModifyTime"
                placeholder="系统自动生成"
                readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label-width="0">
              <div class="jsbos-common-title">
                <h2>答题结果</h2>
              </div>
              <el-table :data="dataForm.temsExamTaskResultList" size="mini">
                <el-table-column
                  type="index"
                  width="50"
                  label="序号"
                  align="center"
                />
                <el-table-column prop="taskId" label="考试记录id">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.taskId"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <!-- <el-table-column prop="parentId" label="案例题父级ID">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.parentId"
                              placeholder="请输入" clearable :style='{"width":"100%"}'>
                    </el-input>
                  </template>
                </el-table-column> -->
                <el-table-column prop="questionId" label="题库试题id">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.questionId"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="questionType" label="题型">
                  <template slot-scope="scope">
                    <el-select
                      v-model="scope.row.questionType"
                      placeholder="请选择"
                      clearable
                      :style="{ width: '100%' }"
                    >
                      <el-option
                        v-for="(item, index) in questionTypeOptions"
                        :key="index"
                        :label="item.fullName"
                        :value="item.id"
                        :disabled="item.disabled"
                      ></el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column prop="title" label="题干">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.title"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="titimgs" label="题干图片视频附件">
                  <template slot-scope="scope">
                    <UploadFz
                      v-model="scope.row.titimgs"
                      :fileSize="5"
                      sizeUnit="MB"
                      :limit="9"
                      buttonText="点击上传"
                    >
                    </UploadFz>
                  </template>
                </el-table-column>
                <el-table-column prop="options" label="选项">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.options"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="opimgs" label="选项图片视频附件">
                  <template slot-scope="scope">
                    <UploadFz
                      v-model="scope.row.opimgs"
                      :fileSize="5"
                      sizeUnit="MB"
                      :limit="9"
                      buttonText="点击上传"
                    >
                    </UploadFz>
                  </template>
                </el-table-column>
                <el-table-column prop="answers" label="正确答案">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.answers"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="score" label="分值">
                  <template slot-scope="scope">
                    <el-input-number
                      v-model="scope.row.score"
                      placeholder="数字文本"
                      :style="{ width: '100%' }"
                      :step="1"
                      controls-position="right"
                    >
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="analysis" label="答案解析">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.analysis"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="analysisimgs"
                  label="答案解析图片视频附件"
                >
                  <template slot-scope="scope">
                    <UploadFz
                      v-model="scope.row.analysisimgs"
                      :fileSize="5"
                      sizeUnit="MB"
                      :limit="9"
                      buttonText="点击上传"
                    >
                    </UploadFz>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="optionRandom"
                  label="选项打乱(0-否，1-是)"
                >
                  <template slot-scope="scope">
                    <el-switch
                      v-model="scope.row.optionRandom"
                      :active-value="1"
                      :inactive-value="0"
                    >
                    </el-switch>
                  </template>
                </el-table-column>
                <el-table-column prop="isMarked" label="是否标记">
                  <template slot-scope="scope">
                    <el-switch
                      v-model="scope.row.isMarked"
                      :active-value="1"
                      :inactive-value="0"
                    >
                    </el-switch>
                  </template>
                </el-table-column>
                <el-table-column prop="myAnswers" label="我的答案">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.myAnswers"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="myScore" label="我的得分">
                  <template slot-scope="scope">
                    <el-input-number
                      v-model="scope.row.myScore"
                      placeholder="数字文本"
                      :style="{ width: '100%' }"
                      :step="1"
                      controls-position="right"
                    >
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="description" label="描述">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.description"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="50">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      class="jsbos-table-delBtn"
                      @click="delTemsExamTaskResultList(scope.$index)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div class="table-actions" @click="addTemsExamTaskResultList()">
                <el-button type="text" icon="el-icon-plus">添加</el-button>
              </div>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        examId: "",
        courseId: "",
        paperId: "",
        startTime: "",
        submitTime: "",
        submitType: "",
        questionReviewStatus: 0,
        reviewerUserId: "",
        reviewerTime: "",
        creatorUserId: "",
        creatorTime: "",
        lastModifyUserId: "",
        lastModifyTime: "",
        temsExamTaskResultList: [],
      },
      rules: {},
      submitTypeOptions: [
        { fullName: "手动交卷", id: "1" },
        { fullName: "自动交卷", id: "2" },
      ],

      questionTypeOptions: [
        { fullName: "单选题", id: "1" },
        { fullName: "多选题", id: "2" },
        {
          fullName: "判断题",
          id: "3",
        },
        { fullName: "填空题", id: "4" },
        { fullName: "问答题", id: "5" },
        // { fullName: "案例题", id: "6" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    temsExamTaskResultExist() {
      let isOk = true;
      for (let i = 0; i < this.dataForm.temsExamTaskResultList.length; i++) {
        const e = this.dataForm.temsExamTaskResultList[i];
      }
      return isOk;
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/exam/TemsExamTask/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          if (!this.temsExamTaskResultExist()) return;
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/exam/TemsExamTask",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/exam/TemsExamTask/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    addTemsExamTaskResultList() {
      let item = {
        taskId: undefined,
        parentId: undefined,
        questionId: undefined,
        questionType: undefined,
        title: undefined,
        titimgs: undefined,
        options: undefined,
        opimgs: undefined,
        answers: undefined,
        score: undefined,
        analysis: undefined,
        analysisimgs: undefined,
        optionRandom: undefined,
        isMarked: undefined,
        myAnswers: undefined,
        myScore: undefined,
        description: undefined,
      };
      this.dataForm.temsExamTaskResultList.push(item);
    },
    delTemsExamTaskResultList(index) {
      this.dataForm.temsExamTaskResultList.splice(index, 1);
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.questionReviewStatus = parseInt(_data.questionReviewStatus);
      for (let i = 0; i < _data.temsExamTaskResultList.length; i++) {
        var _list = _data.temsExamTaskResultList[i];
        _list.titimgs = JSON.stringify(_list.titimgs);
        _list.opimgs = JSON.stringify(_list.opimgs);
        _list.analysisimgs = JSON.stringify(_list.analysisimgs);
        _list.optionRandom = parseInt(_list.optionRandom);
        _list.isMarked = parseInt(_list.isMarked);
      }
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.questionReviewStatus = parseInt(_dataAll.questionReviewStatus);
      for (let i = 0; i < _dataAll.temsExamTaskResultList.length; i++) {
        var _list = _dataAll.temsExamTaskResultList[i];
        _list.titimgs = JSON.parse(_list.titimgs);
        _list.opimgs = JSON.parse(_list.opimgs);
        _list.analysisimgs = JSON.parse(_list.analysisimgs);
        _list.optionRandom = parseInt(_list.optionRandom);
        _list.isMarked = parseInt(_list.isMarked);
      }
      this.dataForm = _dataAll;
    },
  },
};
</script>
